<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="container">
    <div class="title-div">
        <h3>
            监控信息填写
        </h3>
    </div>
    <div >
        <div class="input-group input-group-lg">
            <span class="input-group-addon" id="basic-addon1">供应商&emsp;&emsp;&emsp;</span>
            <input id="supplier" type="text" class="form-control" placeholder="supplier" aria-describedby="basic-addon">
        </div>
        <div class="input-group input-group-lg">
            <span class="input-group-addon" id="basic-addon2">文件路径&emsp;&emsp;</span>
            <input id="filePath" type="text" class="form-control" placeholder="filePath" aria-describedby="basic-addon">
        </div>
        <div id="form-group">
            <div id="template" class="input-group input-group-lg">
                <span class="input-group-addon" id="basic-addon3">文件名&emsp;&emsp;&emsp;</span>
                <input type="text" name="fileName" class="form-control" placeholder="fileName" aria-describedby="basic-addon">

                <span class="input-group-addon" id="basic-addon4">文件到达时间</span>
                <input type="datetime-local" name="arriveTime" class="form-control" placeholder="receiveTime" aria-describedby="basic-addon">

                <span class="input-group-addon">
                    <input name="isContain" type="checkbox" onclick="checkeds(this)" class="form-check-input" value="0">是否包含日期</span>

                <span class="input-group-addon" id="basic-addon6">后缀</span>
                <input type="text" name="suf" class="form-control" placeholder="+1" aria-describedby="basic-addon">

                <span class="input-group-btn">
                <button onclick="addItem()" id="add-item" class="my-btn btn btn-primary" type="button">
							<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
						</button>
                <button onclick="delItem(this)" id="del-item" class="my-btn btn btn-primary" type="button">
							<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
						</button>
            </span>
            </div>
        </div>

        <div class="input-group input-group-lg">
            <span class="input-group-addon" id="basic-addon5">备注&emsp;&emsp;&emsp;&emsp;</span>
            <input id="remarks" type="text" class="form-control" placeholder="remarks" aria-describedby="basic-addon">
        </div>
    </div>
    <div style="text-align: center;margin-top: 20px">
        <button onclick="getFileArry()" class="btn btn-primary btn-lg" type="button">
            提交
        </button>
    </div>

</div>

<style>
    .my-btn {
        border: #0d0d0d 1px solid;
    }

    .title-div {
        text-align: center;
        margin-bottom: 30px;
    }

    .container {
        width: 80%;
        margin-top: 60px;
    }

    .input-group {
        margin-top: 10px;
    }

</style>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
        crossorigin="anonymous"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
        integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
        crossorigin="anonymous"></script>
<script>
    var index = 1;
    function addItem () {
        index++;
        var $template = $('#template'),
            $clone = $template
                .clone()
                .removeAttr('id')
                .attr('data-index', index)
                .insertAfter($template);
        // $clone
        //     .find('[name="pricesTemplate"]').attr('name', 'prices').end()
        //     .find('[name="provincesTemplate"]').attr('name', 'provinces').end();
    };

    function delItem (e) {
        if (index == 1){
           return alert("至少保留一个文件！")
        }
        var target = $(e).parents(".input-group");
        target.remove();
    };

    function checkeds(e) {
        if (e.value == 1) {
            e.value = 0;
            e.checked = 0;
        } else {
            e.value = 1;
        }
    }


    function getFileArry () {
        var fileNames = $("*[name='fileName']").map(function () {
            return $(this).val()
        }).get();
        var arriveTimes = $("*[name='arriveTime']").map(function () {
            return $(this).val()
        }).get();
        var isContains = $("*[name='isContain']").map(function () {
            return $(this).val()
        }).get();

        var sufs = $("*[name='suf']").map(function () {
            return $(this).val()
        }).get();
        var data = [];
        for (var i = 0; i < fileNames.length; i++) {
            console.log(isContains)
            var a = {
                supplier:$("#supplier").val(),
                filePath:$("#filePath").val(),
                fileName:isContains[i]==1?fileNames[i]+"[YYYYMMDD]"+sufs[i]:fileNames[i]+sufs[i],
                receiveTime:arriveTimes[i],
                remarks:$("#remarks").val()
            }
            data.push(a)
        }
        console.log(data)
        $.ajax({
            url: "",
            type: "post",
            data: data,
            contentType:"application/json",
            success: function (result) {
                if (result.code == 200){

                }else{

                }
            },
            error: function (a, b, c) {
            }
        })
    };
</script>
</body>
</html>